package com.bwie.gddproject.index.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bwie.gddproject.R;
import com.bwie.gddproject.event.CidEvent;
import com.bwie.gddproject.index.model.bean.VpBean;
import com.bwie.gddproject.index.view.fragment.ClassifyFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by 我走路带风 on 2017/12/16.
 */

public class Classify1Adapter extends RecyclerView.Adapter {
    private Context context;
    private List<VpBean.DataBean> list;

    public Classify1Adapter(ClassifyFragment classifyFragment, List<VpBean.DataBean> data) {
        this.context = classifyFragment.getActivity();
        this.list = data;
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setIscheck(false);
        }
        list.get(0).setIscheck(true);

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.classify_left, null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final MyHolder myHolder = (MyHolder) holder;
        myHolder.tv.setText(list.get(position).getName());
        if (list.get(position).isIscheck()){
            myHolder.tv.setTextColor(Color.parseColor("#ff0000"));
        }else{
            myHolder.tv.setTextColor(Color.parseColor("#000000"));
        }
        myHolder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CidEvent cidEvent = new CidEvent();
                cidEvent.setCid(list.get(position).getCid() + "");
                EventBus.getDefault().post(cidEvent);
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setIscheck(false);
                }
                list.get(position).setIscheck(true);
                notifyDataSetChanged();
            }
        });


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView tv;
        LinearLayout ll;

        public MyHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.lft_tv);
            ll = itemView.findViewById(R.id.left_ll);
        }
    }

}
